import json
from dataclasses import dataclass

import requests
from bs4 import BeautifulSoup


def get_moive_info(n):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like G Chrome/114.0.0.0 Safari/537.36'}
    url = 'https://movie.douban.com/top250?start=25&filter='
    # 发送 HTTP 请求获取 HTML 内容
    web = requests.get(url, headers=headers)
    soup = BeautifulSoup(web.text, "html.parser")

    li_s = soup.find('ol', class_='grid_view').find_all('li')
    # 遍历所有 <li> 标签并创建 ProductInfo 对象列表
    movie_list = []
    for li in li_s:
        title = li.find(class_='title').string
        point = li.find(class_='rating_num').string
        image_addr = li.find('a').find('img').get('src')
        author = li.find('p').text.strip()
        inq = li.find(class_='inq').text.strip()
        moive = Book(title, point, image_addr, author, inq)
        movie_list.append(moive)
    return movie_list


@dataclass
class Book:
    title: str
    point: float
    image_addr: str
    author: str
    inq: str

    def to_dict(self):
        return {
            'title': self.title,
            'point': self.point,
            'image_addr': self.image_addr,
            'author': self.author,
            'inq': self.inq
        }


if __name__ == '__main__':
    books = get_moive_info(3)
    product_dicts = [t.to_dict() for t in books]
    for product in product_dicts:
        print(product)
   # print(json.dumps(product_dicts, ensure_ascii=False))
    print(1)